<template>
    <div class="app-container">
      <el-row :gutter="20">
<!--        顶部数据加载栏-->
        <el-col :span="24" :xs="24">
          <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
            <el-form-item label="更新时间">
              <el-date-picker
                v-model="dateRange"
                size="small"
                style="width: 240px"
                value-format="yyyy-MM-dd"
                type="daterange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              ></el-date-picker>
            </el-form-item>
            <el-form-item label="学科名称" prop="subject">
              <el-input
                v-model="queryParams.subject"
                placeholder="学科名称"
                clearable
                size="small"
                style="width: 180px"
              />
            </el-form-item>
            <el-form-item label="台站名称" prop="station">
              <el-input  v-model="queryParams.station"
                         placeholder="台站名称"
                         clearable
                         size="small"
                         style="width: 180px"/>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            </el-form-item>
          </el-form>
          <el-form>
            <el-form-item>
              <el-button
                type="primary"
                plain
                icon="el-icon-plus"
                size="mini"
                @click="handleAdd"
                v-hasPermi="['library:zl:add']"
              >新增</el-button>
            </el-form-item>
          </el-form>
          <el-row :gutter="10" class="mb8">
            <el-table size="mini" v-loading="loading" :data="zlList">
              <el-table-column label="编号" align="center" prop="id" v-if="columns[0].visible" />
              <el-table-column label="地震" align="center" prop="eqName" v-if="columns[1].visible" />
              <el-table-column label="学科" align="center" prop="subject" v-if="columns[2].visible" />
              <el-table-column label="台站名称" align="center" key="station" prop="station" v-if="columns[3].visible"/>
              <el-table-column label="测项" align="center" key="cx" prop="cx" v-if="columns[4].visible"/>
              <el-table-column label="分量" align="center" key="fl" prop="fl" v-if="columns[5].visible"/>
              <el-table-column label="tmp" align="center" key="tmp" prop="tmp" v-if="columns[6].visible"/>
              <el-table-column label="no" align="center" key="no" prop="no" v-if="columns[7].visible"/>
              <el-table-column label="路径" align="center" key="path" prop="path" v-if="columns[8].visible" :show-overflow-tooltip="true"/>
              <el-table-column
                label="操作"
                align="center"
                width="160"
                class-name="small-padding fixed-width"
              >
                <template slot-scope="scope">
                  <el-button
                    size="mini"
                    type="text"
                    icon="el-icon-edit"
                    @click="handleUpdate(scope.row)"
                    v-hasPermi="['library:base:edit']"
                  >修改</el-button>
                  <el-button
                    v-if="scope.row.scid !== 1"
                    size="mini"
                    type="text"
                    icon="el-icon-delete"
                    @click="handleDelete(scope.row)"
                    v-hasPermi="['library:base:remove']"
                  >删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <pagination
              v-show="total>0"
              :total="total"
              :page.sync="queryParams.pageNum"
              :limit.sync="queryParams.pageSize"
              @pagination="getList"
            />
          </el-row>
        </el-col>
      </el-row>

      <!-- 添加或修改参数配置对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-row>
            <el-col :span="12">
              <el-form-item label="ID" prop="id">
                <el-input v-model="form.id" readonly="readonly" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="地震" prop="eqName">
                <el-input v-model="form.eqName" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="学科" prop="subject">
                <el-input v-model="form.subject" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="台站" prop="station">
                <el-input v-model="form.station" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="测项" prop="cx">
                <el-input v-model="form.cx" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="分量" prop="fl">
                <el-input v-model="form.fl" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="Tmp" prop="tmp">
                <el-input v-model="form.tmp" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="No" prop="no">
                <el-input v-model="form.no" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24" v-show="isZlHavePath">
              <el-form-item label="文件路径" prop="path">
                <el-input v-model="form.path" readonly="readonly" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item>
                <el-button
                  type="info"
                  plain
                  icon="el-icon-upload2"
                  size="mini"
                  @click="handleImport"
                  v-hasPermi="['library:zl:add']"
                >附件上传</el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>

      <!-- 附件上传对话框 -->
      <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
        <el-upload
          ref="upload"
          :limit="1"
          accept=".xlsx, .xls, .doc, .docx"
          :headers="upload.headers"
          :action="upload.url + '?updateSupport=' + upload.updateSupport"
          :disabled="upload.isUploading"
          :on-progress="handleFileUploadProgress"
          :on-success="handleFileSuccess"
          :auto-upload="false"
          drag
        >
          <i class="el-icon-upload"></i>
          <div class="el-upload__text">
            将文件拖到此处，或
            <em>点击上传</em>
          </div>
          <div class="el-upload__tip" style="color:red" slot="tip">提示：仅允许导入“xls”或“xlsx”或“doc”或“docx”格式文件！</div>
        </el-upload>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitFileForm">确 定</el-button>
          <el-button @click="upload.open = false">取 消</el-button>
        </div>
      </el-dialog>
    </div>
</template>

<script>
  import { listZlLib,updateZlLib,getOneZlLib,addZlLib,delZlLib } from "@/api/zllib/zlData";
  import { getToken } from "@/utils/auth";
  export default {
    name: "index",
    data(){
      return{
        // 遮罩层
        loading: true,
        // 台站数据
        zlList: null,
        // 日期范围
        dateRange: [],
        // 是否显示弹出层
        open: false,
        // 表单参数
        form: {},
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          orderByColumn:"id",
          isAsc:"desc",
          subject:"",
          station:"",
        },
        // 文件上传参数
        upload: {
          // 是否显示弹出层（附件上传）
          open: false,
          // 弹出层标题（附件上传）
          title: "",
          // 是否禁用上传
          isUploading: false,
          // 是否更新已经存在的用户数据
          updateSupport: 0,
          // 设置上传的请求头部
          headers: { Authorization: "Bearer " + getToken() },
          // 上传的地址
          url: process.env.VUE_APP_BASE_API + "/common/upload",
          fileList:[]
        },
        isZlHavePath:false,//是否生成了文件路径
        // 状态数据字典
        statusOptions: [],
        // 列信息
        columns: [
          { key: 0, label: `编号`, visible: false },
          { key: 1, label: `地震`, visible: true },
          { key: 2, label: `学科`, visible: true },
          { key: 3, label: `台站名称`, visible: true },
          { key: 4, label: `测项`, visible: true },
          { key: 5, label: `分量`, visible: true },
          { key: 6, label: `tmp`, visible: true },
          { key: 7, label: `no`, visible: true },
          { key: 8, label: `路径`, visible: true },
        ],
        // 表单校验
        rules: {
          station: [
            { required: true, message: "台站名称不能为空", trigger: "blur" }
          ],
          subject: [
            { required: true, message: "测项名称不能为空", trigger: "blur" }
          ],
        },
        // 总条数
        total: 0,
        // 弹出层标题
        title: "",
      }
    },
    created() {
      // this.loading = false;
      this.getList();
    },
    methods:{
      getList(){
        this.loading = true;
        listZlLib(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
            this.zlList = response.rows;
            this.total = response.total;
            this.loading = false;
          }
        );
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const staId = row.id;
        getOneZlLib(staId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改日志记录";
        });
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "新增日志记录";
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const staIds = row.id || this.ids;
        this.$confirm('是否确认删除编号为"' + staIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delZlLib(staIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 提交按钮 */
      submitForm: function() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != undefined) {
              updateZlLib(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addZlLib(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 导入按钮操作 */
      handleImport() {
        this.upload.title = "附件上传";
        this.upload.open = true;
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          eqName: undefined,
          subject: undefined,
          station: undefined,
          cx: undefined,
          fl:undefined,
          tmp:undefined,
          no:undefined,
          path:undefined,
        };
        this.resetForm("form");
      },
      // 文件上传中处理
      handleFileUploadProgress(event, file, fileList) {
        this.upload.isUploading = true;
      },
      // 文件上传成功处理
      handleFileSuccess(response, file, fileList) {
        this.upload.open = false;
        this.upload.isUploading = false;
        this.isZlHavePath = true;
        this.form.path = "D:/ruoyi/uploadPath/"+response.fileName.substr(7);
        this.$refs.upload.clearFiles();
        this.$alert(response.msg, "上传结果", { dangerouslyUseHTMLString: true });
        this.getList();
      },
      // 提交上传文件
      submitFileForm() {
        this.$refs.upload.submit();
      }
    }
  }
</script>

<style scoped>

</style>
